﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using CPETwit.WPFClient.Business.TwitterServiceReference;
using CPETwit.WPFClient.ViewModel;
using CPETwit.WPFClient.Business.Service;

namespace CPETwit.WPFClient.Common
{
    public static class DialogManager
    {

        public static void ShowMainMenu()
        {
            MainWindow window = App.Window;
            window.mainMenu.Visibility = Visibility.Visible;
            window.loginMenu.Visibility = Visibility.Collapsed;
            window.profileMenu.Visibility = Visibility.Collapsed;
            window.researchMenu.Visibility = Visibility.Collapsed;

            window.Left = (SystemParameters.PrimaryScreenWidth - (double)window.GetValue(Window.WidthProperty)) / 4;
            window.Top = (SystemParameters.PrimaryScreenHeight - (double)window.GetValue(Window.HeightProperty)) / 6;

            UpdateTweetMenus();
            UpdateMainTimeLine();
        }

        public static void ShowProfileMenu(User currentUser)
        {
            MainWindow window = App.Window;
            window.mainMenu.Visibility = Visibility.Collapsed;
            window.loginMenu.Visibility = Visibility.Collapsed;
            window.profileMenu.Visibility = Visibility.Visible;
            window.researchMenu.Visibility = Visibility.Collapsed;

            HideProfileEdition();
            UpdateProfileEditing();
            UpdateProfileDisplaying(currentUser);
            UpdateProfileTimeLine(currentUser);
        }

        public static void ShowAuthentificationMenu()
        {
            MainWindow window = App.Window;
            window.mainMenu.Visibility = Visibility.Collapsed;
            window.loginMenu.Visibility = Visibility.Visible;
            window.profileMenu.Visibility = Visibility.Collapsed;
            window.researchMenu.Visibility = Visibility.Collapsed;

            window.loginMenu.authentification.txtbxLogin.ProcessedText = string.Empty;
            window.loginMenu.authentification.txtbxPassword.Password = string.Empty;
        }

        public static void ShowUsersResearch()
        {
            MainWindow window = App.Window;
            window.mainMenu.Visibility = Visibility.Collapsed;
            window.loginMenu.Visibility = Visibility.Collapsed;
            window.profileMenu.Visibility = Visibility.Collapsed;
            window.researchMenu.Visibility = Visibility.Visible;
            window.researchMenu.researchUsers.Visibility = Visibility.Visible;
            window.researchMenu.researchTweets.Visibility = Visibility.Collapsed;
            UpdateResearchUsers();
        }

        public static void ShowTweetResearch(string research)
        {
            MainWindow window = App.Window;
            window.mainMenu.Visibility = Visibility.Collapsed;
            window.loginMenu.Visibility = Visibility.Collapsed;
            window.profileMenu.Visibility = Visibility.Collapsed;
            window.researchMenu.Visibility = Visibility.Visible;
            window.researchMenu.researchUsers.Visibility = Visibility.Collapsed;
            window.researchMenu.researchTweets.Visibility = Visibility.Visible;

            UpdateResearchTweets(research);
        }

        public static void ShowProfileEdition()
        {
            MainWindow window = App.Window;
            window.profileMenu.profileEditing.Visibility = Visibility.Visible;
            window.profileMenu.timelineProfile.Visibility = Visibility.Collapsed;
        }
        public static void HideProfileEdition()
        {
            MainWindow window = App.Window;
            window.profileMenu.profileEditing.Visibility = Visibility.Collapsed;
            window.profileMenu.timelineProfile.Visibility = Visibility.Visible;
        }

        public static void UpdateTweetMenus()
        {
            MainWindow window = App.Window;

            TweetMenuViewModel tweetMenuViewModel = (window.mainMenu.tweetMenu.DataContext as TweetMenuViewModel);
            if (tweetMenuViewModel != null)
                tweetMenuViewModel.UpdateViewModel(CommonData.LOGGED_USER);

            TweetMenuViewModel tweetMenuViewModel2 = (window.profileMenu.tweetMenu.DataContext as TweetMenuViewModel);
            if (tweetMenuViewModel2 != null)
                tweetMenuViewModel2.UpdateViewModel(CommonData.LOGGED_USER);
        }

        public static void UpdateMainTimeLine()
        {
            MainWindow window = App.Window;

            TimelineViewModel timelineProfileViewModel =
                window.mainMenu.timeline.DataContext as TimelineViewModel;

            if (timelineProfileViewModel != null)
                timelineProfileViewModel.UpdateViewModel(CommonData.LOGGED_USER, true);
        }

        public static void UpdateProfileTimeLine(User user)
        {
            MainWindow window = App.Window;

            TimelineViewModel timelineProfileViewModel =
                window.profileMenu.timelineProfile.DataContext as TimelineViewModel;

            if (timelineProfileViewModel != null)
                timelineProfileViewModel.UpdateViewModel(user, false);
        }

        public static void UpdateProfileEditing()
        {
            MainWindow window = App.Window;
            ProfileEditingViewModel model = window.profileMenu.profileEditing.DataContext as ProfileEditingViewModel;
            if (model != null)
                model.UpdateViewModel(CommonData.LOGGED_USER);
        }

        public static void UpdateProfileDisplaying(User user)
        {
            MainWindow window = App.Window;
            ProfileDisplayingViewModel profileDisplayingViewModel =
                window.profileMenu.profileDisplaying.DataContext as ProfileDisplayingViewModel;
            if (profileDisplayingViewModel != null)
                profileDisplayingViewModel.UpdateViewModel(user);
        }

        public static void UpdateResearchUsers()
        {
            MainWindow window = App.Window;
            ResearchUsersViewModel researchUsersViewModel =
                (window.researchMenu.researchUsers.DataContext as ResearchUsersViewModel);
            if (researchUsersViewModel != null)
                researchUsersViewModel.UpdateViewModel();
        }

        public static void UpdateResearchTweets(string research)
        {
            MainWindow window = App.Window;
            TimelineViewModel researchTweetsViewModel =
                (window.researchMenu.researchTweets.DataContext as TimelineViewModel);
            if (researchTweetsViewModel != null)
                researchTweetsViewModel.UpdateViewModelByResearch(research);
        }

        public static void SetManBackground()
        {
            MainWindow window = App.Window;
            window.Background = new SolidColorBrush(Color.FromArgb(255, 118, 141, 255));
        }
        public static void SetWomanBackGround()
        {
            MainWindow window = App.Window;
            window.Background = new SolidColorBrush(Color.FromArgb(245, 239, 136, 255));
        }

        public static void SetUnknownBackGround()
        {
            MainWindow window = App.Window;
            window.Background = new SolidColorBrush(Colors.Gray);
        }

        public static void ChangeBackGround()
        {
            if (CommonData.LOGGED_USER.sex == "M")
                SetManBackground();
            else if (CommonData.LOGGED_USER.sex == "F")
                SetWomanBackGround();
            else
                SetUnknownBackGround();
        }
    }
}
